import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/a_button/index.dart';
import 'package:im_flutter_sdk_example/components/labels/index.dart';
import 'package:im_flutter_sdk_example/jsonserialize/serve/data.dart';
import 'package:im_flutter_sdk_example/pages/news/chat_input.dart';
import 'package:im_flutter_sdk_example/pages/news/comment.dart';
import 'package:im_flutter_sdk_example/pages/serve/widgets/header.dart';
import 'package:im_flutter_sdk_example/pages/serve/widgets/introduce.dart';
import 'package:im_flutter_sdk_example/pages/serve/widgets/main_page.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/serve_list.dart';
import 'package:im_flutter_sdk_example/provider/comment_model.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:provider/provider.dart';

// ignore: must_be_immutable
class ServeDetail extends StatefulWidget {
  ServeDetail({Key key}) : super(key: key);

  @override
  _ServeDetailState createState() => _ServeDetailState();
}

class _ServeDetailState extends State<ServeDetail> {
  bool isCollect = false; //是否收藏
  bool isToComment = false; //是否点击评论
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      final ServeData item = ModalRoute.of(context).settings.arguments;
      loadIsCollect(item.serviceReleaseId.toString());
    });
  }

  @override
  Widget build(BuildContext context) {
    final ServeData item = ModalRoute.of(context).settings.arguments;
    return Scaffold(
      appBar: AppBar(title: Text('服务详情')),
      body: SafeArea(
        child: Stack(
          children: [
            Positioned(
              child: SingleChildScrollView(
                child: InkWell(
                  onTap: () {
                    cancelInputView();
                  },
                  child: Container(
                    color: rgba(249, 249, 249, 1),
                    padding: EdgeInsets.only(
                      bottom: 58,
                    ),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Header(item: item),
                          MainPage(item: item),
                          Introduction(item: item),
                          Comment(
                              id: item.serviceReleaseId,
                              type: 3,
                              getFocus: openInputView),
                          LabelSimlarRecommend(),
                          ServeList(isRecommend: true),
                          LabelLoadAll()
                        ]),
                  ),
                ),
              ),
            ),
            Positioned(
                child: Container(
                    width: G.screenWidth(),
                    padding:
                        EdgeInsets.only(top: 8, bottom: 10, left: 8, right: 10),
                    // color: Colors.red,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        boxShadow: [
                          BoxShadow(
                              color: Colors.white10, offset: Offset(0, 10)),
                        ],
                        border: Border(
                            top: BorderSide(
                                width: 1, color: rgba(235, 235, 235, 1)))),
                    child: Container(
                      child: isToComment
                          ? ChatInput(
                              type: 3,
                              id: item.serviceReleaseId,
                              cancelInputView: cancelInputView)
                          : Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Expanded(
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Image.asset(
                                          'lib/assets/images/footer_share.png',
                                          width: 22,
                                          height: 22),
                                      isCollect
                                          ? InkWell(
                                              onTap: () {
                                                cancelCollect(item
                                                    .serviceReleaseId
                                                    .toString());
                                              },
                                              child: Image.asset(
                                                  'lib/assets/images/footer_star_active.png',
                                                  width: 22,
                                                  height: 22),
                                            )
                                          : InkWell(
                                              onTap: () {
                                                addCollect(item.serviceReleaseId
                                                    .toString());
                                              },
                                              child: Image.asset(
                                                  'lib/assets/images/footer_star.png',
                                                  width: 22,
                                                  height: 22),
                                            ),
                                      InkWell(
                                        onTap: () {
                                          openInputView();
                                        },
                                        child: Image.asset(
                                            'lib/assets/images/footer_chat.png',
                                            width: 22,
                                            height: 22),
                                      ),
                                    ],
                                  ),
                                ),
                                Row(
                                  children: [
                                    AButton.normal(
                                        width: 106,
                                        height: 40,
                                        bgColor: rgba(235, 101, 13, 1),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            Padding(
                                              padding: const EdgeInsets.only(
                                                  left: 10),
                                              child: Text('立即购买'),
                                            ),
                                          ],
                                        ),
                                        type: 'primary',
                                        onPressed: () {
                                          G.pushNamed('/serve_sure');
                                        }),
                                    Padding(
                                      padding: EdgeInsets.only(left: 6),
                                      child: AButton.normal(
                                          width: 106,
                                          height: 40,
                                          child: Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Padding(
                                                padding: const EdgeInsets.only(
                                                    left: 10),
                                                child: Text('我要咨询'),
                                              ),
                                            ],
                                          ),
                                          bgColor: rgba(42, 125, 250, 1),
                                          type: 'primary',
                                          onPressed: () {
                                            G.pushNamed('/serve_sure');
                                          }),
                                    ),
                                  ],
                                )
                              ],
                            ),
                    )),
                bottom: 0)
          ],
        ),
      ),
    );
  }

  //是否收藏
  void loadIsCollect(String id) async {
    var result = await G.req.news.reqNewsIsCollect(objType: 3, objId: id);
    print('项目是否收藏$result');
    if (result.data['status'] == 1) {
      isCollect = result.data['data'];
      this.setState(() {});
    }
  }

  //收藏
  void addCollect(id) async {
    print('$id');
    var result = await G.req.news.addNewsCollect(objId: id, objType: 3);
    print('收藏结果$result');
    if (result.data['status'] == 1) {
      G.toast('收藏成功');
      loadIsCollect(id);
    }
  }

  //取消收藏
  void cancelCollect(id) async {
    print('$id');
    var result = await G.req.news.cancelNewsCollect(objId: id, objType: 3);
    print('收藏结果$result');
    if (result.data['status'] == 1) {
      G.toast('取消收藏');
      loadIsCollect(id);
    }
  }

  //关闭输入界面
  void cancelInputView() {
    if (isToComment == true) {
      CommentModelProvider _formData =
          Provider.of<CommentModelProvider>(context);
      _formData.resetPlaceHolder();
      isToComment = false;
      this.setState(() {});
    }
  }

  //打开输入界面
  void openInputView() {
    isToComment = true;
    CommentModelProvider _formData = Provider.of<CommentModelProvider>(context);
    _formData.resetPlaceHolder();
    this.setState(() {});
  }
}
